//
//  YiPaiDetailViewController.m
//  YJRenter
//
//  Created by 文彬 on 2018/7/11.
//  Copyright © 2018年 文彬. All rights reserved.
//

#import "YiPaiDetailViewController.h"
#import "HouseDetailNavView.h"
#import "YiPaiDetailHeaderView.h"
#import "HouseDetailUserInfoTableViewCell.h"
#import "PriceRecordViewController.h"
#import "LandlordHomeViewController.h"

@interface YiPaiDetailViewController ()<UITableViewDelegate,
    UITableViewDataSource>

@property (strong, nonatomic) HouseDetailNavView *navView;
@property (strong, nonatomic) YiPaiDetailHeaderView *topView;
@property (strong, nonatomic) UITableView *listTableView;

@end

@implementation YiPaiDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    [self initPageControl];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.navigationController setNavigationBarHidden:YES animated:animated];
}

- (void)viewWillDisappear:(BOOL)animated{
    [super viewWillDisappear:animated];
    [self.navigationController setNavigationBarHidden:NO animated:animated];
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (void)initPageControl{
    
    [self.view addSubview:self.listTableView];
    [self.listTableView makeConstraints:^(MASConstraintMaker *make) {
        make.left.top.right.equalTo(@0);
        make.bottom.equalTo(@-50);
    }];
    self.listTableView.tableHeaderView = self.topView;
    
    [self.view addSubview:self.navView];
    
    [StaticTools addPageFootButton:self.view title:@"出价" titleFont:[UIFont systemFontOfSize:16] backgroundColor:kMainOriginColor titleColor:[UIColor whiteColor] buttonHeight:45 bottomMargin:5 leftMargin:10 cornerRadius:5 click:^(id param) {
        
    }];
    
}

#pragma mark HouseFilterViewDelegate
- (void)houseDetailNavClick:(NSInteger)index{
    if (index==10) {
        [self.navigationController popViewControllerAnimated:YES];
    }
}
#pragma mark ScrollView
- (void)scrollViewDidScroll:(UIScrollView *)scrollView{
    
    [self.navView scrollOffset:scrollView.contentOffset.y];
}

#pragma mark UITableView
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    return section==1?3:1;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 10;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==2) {
        return 110;
    }
    else if(indexPath.section==1){
        if (indexPath.row==0) {
            return 40;
        }
        else if(indexPath.row==1){
            return 80;
        }
        else if(indexPath.row==2){
            return 100;
        }
    }
    return 60;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{

    if (indexPath.section==2) {
        HouseDetailUserInfoTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"HouseDetailUserInfoTableViewCell" forIndexPath:indexPath];
        [cell.imgView  setRoundImageWithURL:@"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=4036889747,3526425271&fm=27&gp=0.jpg" placeholderImage:nil];
        return cell;
    }
    
    
    static NSString *iden = @"iden";
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden];
    if (cell==nil) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:iden];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    for (UIView *view in cell.contentView.subviews) {
        [view removeFromSuperview];
    }
    
    if (indexPath.section==0) {
        
        UILabel *messLabel = [[UILabel alloc]init];
        messLabel.font = [UIFont systemFontOfSize:15];
        messLabel.textColor = kSecTitleColor;
        messLabel.text = @"租金价";
        [cell.contentView addSubview:messLabel];
        [messLabel makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@10);
            make.centerY.equalTo(cell.contentView.centerY);
            make.width.equalTo(@60);
            make.height.equalTo(@30);
        }];
        
        UILabel *moneyLabel = [[UILabel alloc]init];
        moneyLabel.font = [UIFont systemFontOfSize:16];
        moneyLabel.textColor = kTitleColor;
        moneyLabel.text = @"￥200.00/天";
        [cell.contentView addSubview:moneyLabel];
        [moneyLabel makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(@60);
            make.centerY.equalTo(cell.contentView.centerY);
            make.width.equalTo(@200);
            make.height.equalTo(@30);
        }];
    }
    else if(indexPath.section==1){
        if (indexPath.row==0) {
            
            UILabel *countLabel = [[UILabel alloc]init];
            countLabel.font = [UIFont systemFontOfSize:16];
            countLabel.textColor = kSecTitleColor;
            countLabel.text = @"竞买记录(8)";
            [cell.contentView addSubview:countLabel];
            [countLabel makeConstraints:^(MASConstraintMaker *make) {
                make.left.equalTo(@10);
                make.centerY.equalTo(cell.contentView.centerY);
                make.width.equalTo(@260);
                make.height.equalTo(@30);
            }];
            
            UILabel *allLabel = [[UILabel alloc]init];
            allLabel.font = [UIFont systemFontOfSize:15];
            allLabel.textColor = kMainOriginColor;
            allLabel.textAlignment = NSTextAlignmentRight;
            allLabel.text = @"全部";
            [cell.contentView addSubview:allLabel];
            [allLabel makeConstraints:^(MASConstraintMaker *make) {
                make.right.equalTo(@-20);
                make.centerY.equalTo(cell.contentView.centerY);
                make.width.equalTo(@50);
                make.height.equalTo(@30);
            }];
            
            UIView *lineView = [[UIView alloc]init];
            lineView.backgroundColor = kLineColor;
            [cell.contentView addSubview:lineView];
            [lineView makeConstraints:^(MASConstraintMaker *make) {
                make.left.bottom.right.equalTo(@0);
                make.height.equalTo(@0.5);
            }];
        }
        else if(indexPath.row==1){
            
            UIView *lineView = [[UIView alloc]init];
            lineView.backgroundColor = kLineColor;
            [cell.contentView addSubview:lineView];
            [lineView makeConstraints:^(MASConstraintMaker *make) {
                make.left.bottom.right.equalTo(@0);
                make.height.equalTo(@0.5);
            }];
            
            float itemWidth = 100;
            float itemDis = (SCREEN_WIDTH-10*2-itemWidth*3)/2;
            for (int i=0; i<3; i++) {
                
                UIView *bgView = [[UIView alloc]init];
                bgView.layer.cornerRadius = 3;
                bgView.layer.borderColor = kSecTitleColor.CGColor;
                bgView.layer.borderWidth = 0.5;
                [cell.contentView addSubview:bgView];
                [bgView makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(@(10+(itemWidth+itemDis)*i));
                    make.height.equalTo(@50);
                    make.centerY.equalTo(cell.contentView.centerY);
                    make.width.equalTo(@(itemWidth));
                }];
                
                UILabel *statueLabel = [[UILabel alloc]init];
                statueLabel.font = [UIFont systemFontOfSize:14];
                statueLabel.backgroundColor = i==0?kMainOriginColor:[UIColor lightGrayColor];
                statueLabel.text = i==0?@"领先":@"出局";
                statueLabel.textColor = [UIColor whiteColor];
                [cell.contentView addSubview:statueLabel];
                [statueLabel makeConstraints:^(MASConstraintMaker *make) {
                    make.left.equalTo(bgView.left);
                    make.centerY.equalTo(bgView.top);
                    make.width.equalTo(@30);
                    make.height.equalTo(@20);
                }];
                
                UILabel *numberLabel = [[UILabel alloc]init];
                numberLabel.font = [UIFont systemFontOfSize:14];
                numberLabel.text = @"T2838";
                numberLabel.textColor = kTitleColor;
                numberLabel.textAlignment = NSTextAlignmentRight;
                [bgView addSubview:numberLabel];
                [numberLabel makeConstraints:^(MASConstraintMaker *make) {
                    make.right.equalTo(@-5);
                    make.top.equalTo(@5);
                    make.width.equalTo(@(itemWidth));
                    make.height.equalTo(@20);
                }];
                
                UILabel *moneyLabel = [[UILabel alloc]init];
                moneyLabel.font = [UIFont systemFontOfSize:14];
                moneyLabel.text = @"￥12.00";
                moneyLabel.textColor = kTitleColor;
                moneyLabel.textAlignment = NSTextAlignmentRight;
                [bgView addSubview:moneyLabel];
                [moneyLabel makeConstraints:^(MASConstraintMaker *make) {
                    make.right.equalTo(@-5);
                    make.bottom.equalTo(@-5);
                    make.width.equalTo(@(itemWidth));
                    make.height.equalTo(@20);
                }];
            }
        }
        else if(indexPath.row==2){
            
            for (int i=0; i<2; i++) {
                for (int j=0; j<2; j++) {
                    
                    UILabel *messLabel = [[UILabel alloc]init];
                    messLabel.font = [UIFont systemFontOfSize:15];
                    messLabel.textColor = kSecTitleColor;
                    messLabel.text = @"竞价周期";
                    [cell.contentView addSubview:messLabel];
                    [messLabel makeConstraints:^(MASConstraintMaker *make) {
                        make.left.equalTo(@(j==0?10:SCREEN_WIDTH/2));
                        make.top.equalTo(@(i==0?20:60));
                        make.width.equalTo(@65);
                        make.height.equalTo(@30);
                    }];
                    
                    UILabel *infoLabel = [[UILabel alloc]init];
                    infoLabel.font = [UIFont systemFontOfSize:15];
                    infoLabel.textColor = kTitleColor;
                    infoLabel.text = @"1天";
                    [cell.contentView addSubview:infoLabel];
                    [infoLabel makeConstraints:^(MASConstraintMaker *make) {
                        make.left.equalTo(messLabel.right);
                        make.centerY.equalTo(messLabel.centerY);
                        make.width.equalTo(@100);
                        make.height.equalTo(@30);
                    }];
                }
            }
        }
    }
    
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section==1&&indexPath.row==0) {
        PriceRecordViewController *recordController = [[PriceRecordViewController alloc]init];
        [self.navigationController pushViewController:recordController animated:YES];
    }
    else if(indexPath.section==2){
        LandlordHomeViewController *homeController = [[LandlordHomeViewController alloc]init];
        [self.navigationController pushViewController:homeController animated:YES];
    }
}
#pragma mark getter
- (UITableView*)listTableView{
    if (_listTableView==nil) {
        _listTableView = [[UITableView alloc]initWithFrame:CGRectZero style:UITableViewStyleGrouped];
        _listTableView.delegate = self;
        _listTableView.dataSource = self;
        _listTableView.sectionFooterHeight = 0;
        _listTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
        _listTableView.backgroundColor = [UIColor clearColor];
        _listTableView.separatorStyle = UITableViewCellSeparatorStyleNone;
           [_listTableView registerNib:[UINib nibWithNibName:@"HouseDetailUserInfoTableViewCell" bundle:nil] forCellReuseIdentifier:@"HouseDetailUserInfoTableViewCell"];
        
    }
    
    return _listTableView;
}

- (HouseDetailNavView*)navView{
    if (_navView==nil) {
        _navView = [[HouseDetailNavView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
        _navView.delegate = self;
    }
    return _navView;
}

- (YiPaiDetailHeaderView*)topView{
    if (_topView==nil) {
        _topView = [[YiPaiDetailHeaderView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 250+110)];
    }
    return _topView;
}

@end
